package framework.drawer.swing.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRootPane;
import javax.swing.SwingUtilities;
import javax.swing.border.MatteBorder;
import javax.swing.plaf.PanelUI;

import com.l2fprod.common.swing.JTaskPane;
import com.l2fprod.common.swing.JTaskPaneGroup;

public class SideBarUI extends PanelUI {
	
    private SideBar sideBar;
    private JTaskPane taskPane;

    public SideBarUI(SideBar sideBar) {
        this.sideBar = sideBar;
    }

    public void installUI(JComponent c) {
        c.removeAll();
        c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS));
        this.taskPane = new JTaskPane();
        /*addElementToTaskPane(this.sideBar.getEditorToolsBar().getAWTComponent(), standardButtonsTitle);
        for (ISideBarElement anExternalElement : this.sideBar.getExternalContributionElements().keySet()) {
            String externalElementTitle = this.sideBar.getExternalContributionElements().get(anExternalElement);
            addElementToTaskPane(anExternalElement.getAWTComponent(), externalElementTitle);
        }*/
        List<EditorToolsPanel> panels = this.sideBar.getToolsPanels();
        for(EditorToolsPanel panel : panels) {
        	addElementToTaskPane(panel.getAWTComponent(), panel.getTitle());
        }
        
        c.add(taskPane);
        c.setBorder(new MatteBorder(0, 1, 0, 0, Color.BLUE));
        fixWidth();
        this.sideBar.doLayout();
        this.sideBar.repaint();
        JRootPane rootPane = SwingUtilities.getRootPane(this.sideBar);
        if (rootPane != null) {
            rootPane.repaint();
        }
    }

    private void fixWidth() {
        JLabel sizer = new JLabel();
        sizer.setPreferredSize(new Dimension(215, 1));
        this.taskPane.add(sizer);
    }

    private void addElementToTaskPane(final Component c, String title) {
        JTaskPaneGroup group = new JTaskPaneGroup();
        group.setFont(group.getFont().deriveFont(Font.PLAIN));
        group.setTitle(title);
        group.setLayout(new BorderLayout());
        group.add(c, BorderLayout.CENTER);
        this.taskPane.add(group);
    }
}
